<?php


namespace app\admin\product\model;

use app\common\Player\K3;
use app\common\Player\Kl10f;
use app\common\Player\Kl8;
use app\common\Player\Lhc;
use app\common\Player\Pc28;
use app\common\Player\Pk10;
use app\common\Player\Ssc;
use app\common\Player\X5;
use Exception;
use mb\helper\Collection;
use think\facade\Cache;
use think\Db;

class Game
{
    protected static $table = 'product_games';

    public static $field = [
        'k3' => [
            'sum' => '和值',
            'k33tx' => '三同号通选',
            'k33dx' => '三同号单选',
            'k33th' => '三不同号',
            'k33lh' => '三连号通选',
            'k32fx' => '二同号复选',
            'k32dx' => '二同号单选',
            'k32th' => '二不同号',
        ],
        'pk10' => [
            'position' => '定位胆',
        ],
        'x5' => [
            'q3zxfs' => '前三直选复式',
        ],
        'kl8' => [
            'rx1' => '任选1',
        ],
        'pl3' => [
            'yxdwdfs' => '一星定位胆复式',
        ],
        'pc28' => [
            'position' => '定位球',
            'special' => '特码'
        ]
    ];


    public static function getPlayer($type)
    {
        $game = [
            'pk10' => [
                'q5zxfs' => [
                    'title' => lang('1-5'),
                    'games' => Pk10::getGames('q5zxfs')
                ],
                'h5zxfs' => [
                    'title' => lang('6-10'),
                    'games' => Pk10::getGames('h5zxfs')
                ],
                'face2' => [
                    'title' => lang('两面'),
                    'games' => Pk10::getGames('face2')
                ],
                'sum' => [
                    'title' => lang('冠亚和'),
                    'games' => Pk10::getGames('sum')
                ],
            ],
            'pc28' => [
                'position' => [
                    'title' => lang('定位球'),
                    'games' => Pc28::getGames('position'),
                ],
                'special' => [
                    'title' => lang('特码'),
                    'games' => Pc28::getGames('special'),
                ],
            ],
            'kl10f' => [
                'face2' => [
                    'title' => lang('两面'),
                    'games' => Kl10f::getGames('face2'),
                ],
                'ball1' => [
                    'title' => lang('第一球'),
                    'games' => Kl10f::getGames('ball1'),
                ],
                'ball2' => [
                    'title' => lang('第二球'),
                    'games' => Kl10f::getGames('ball2'),
                ],
                'ball3' => [
                    'title' => lang('第三球'),
                    'games' => Kl10f::getGames('ball3'),
                ],
                'ball4' => [
                    'title' => lang('第四球'),
                    'games' => Kl10f::getGames('ball4'),
                ]
                ,'ball5' => [
                    'title' => lang('第五球'),
                    'games' => Kl10f::getGames('ball5'),
                ],
                'ball6' => [
                    'title' => lang('第六球'),
                    'games' => Kl10f::getGames('ball6'),
                ],
                'ball7' => [
                    'title' => lang('第七球'),
                    'games' => Kl10f::getGames('ball7'),
                ],
                'ball8' => [
                    'title' => lang('第八球'),
                    'games' => Kl10f::getGames('ball8'),
                ],
            ],

            'k3' => [
                'three' => [
                    'title' => lang('三军'),
                    'games' => K3::getGames('three'),
                ],
                'ws' => [
                    'title' => lang('围骰'),
                    'games' => K3::getGames('ws'),
                ],
                'point' => [
                    'title' => lang('点数'),
                    'games' => K3::getGames('point'),
                ],
                'long' => [
                    'title' => lang('长牌'),
                    'games' => K3::getGames('long'),
                ],
                'short' => [
                    'title' => lang('短牌'),
                    'games' => K3::getGames('short'),
                ],
            ],

            'ssc' => [
                'face2' => [
                    'title' => lang('两面'),
                    'games' => Ssc::getGames('face2'),
                ],
                'position' => [
                    'title' => lang('定位球'),
                    'games' => Ssc::getGames('position'),
                ],
                'qzh' => [
                    'title' => lang('前中后'),
                    'games' => Ssc::getGames('qzh'),
                ],
                'lhd' => [
                    'title' => lang('龙虎斗'),
                    'games' => Ssc::getGames('lhd'),
                ],
                'bjl' => [
                    'title' => lang('百家乐'),
                    'games' => Ssc::getGames('bjl'),
                ],
                'dn' => [
                    'title' => lang('斗牛'),
                    'games' => Ssc::getGames('dn'),
                ],
//                'lhd' => [
//                    'title' => lang('百家乐'),//todo 百家乐
//                ],
            ],
            'kl8' => [
                'face2' => [
                    'title' => lang('两面'),
                    'games' => Kl8::getGames('face2'),
                ],
                'wx' => [
                    'title' => lang('五行'),
                    'games' => Kl8::getGames('wx'),
                ],
                'zm' => [
                    'title' => lang('正码'),
                    'games' => Kl8::getGames('zm'),
                ],
            ],

            'x5' => [
                'face2' => [
                    'title' => lang('两面'),
                    'games' => X5::getGames('face2'),
                ],
                'ball1' => [
                    'title' => lang('第一球'),
                    'games' => X5::getGames('ball1'),
                ],
                'ball2' => [
                    'title' => lang('第二球'),
                    'games' => X5::getGames('ball2'),
                ],
                'ball3' => [
                    'title' => lang('第三球'),
                    'games' => X5::getGames('ball3'),
                ],
                'ball4' => [
                    'title' => lang('第四球'),
                    'games' => X5::getGames('ball4'),
                ],
                'ball5' => [
                    'title' => lang('第五球'),
                    'games' => X5::getGames('ball5'),
                ],
            ],
            'lhc' => [
                'tm' => [
                    'title' => lang('特码'),
                    'games' => Lhc::getGames('tm'),
                ],
                'face2' => [
                    'title' => lang('两面'),
                    'games' => Lhc::getGames('face2'),
                ],
                'zm' => [
                    'title' => lang('正码'),
                    'games' => Lhc::getGames('zm'),
                ],
                'z1' => [
                    'title' => lang('正1'),
                    'games' => Lhc::getGames('z1'),
                ],
                'z2' => [
                    'title' => lang('正2'),
                    'games' => Lhc::getGames('z2'),
                ],
                'z3' => [
                    'title' => lang('正3'),
                    'games' => Lhc::getGames('z3'),
                ],
                'z4' => [
                    'title' => lang('正4'),
                    'games' => Lhc::getGames('z4'),
                ],
                'z5' => [
                    'title' => lang('正5'),
                    'games' => Lhc::getGames('z5'),
                ],
                'z6' => [
                    'title' => lang('正6'),
                    'games' => Lhc::getGames('z6'),
                ],
                'sb' => [
                    'title' => lang('色波'),
                    'games' => Lhc::getGames('sb'),
                ],
                'zx' => [
                    'title' => lang('总肖'),
                    'games' => Lhc::getGames('zx'),
                ],
                'zhengx' => [
                    'title' => lang('正肖'),
                    'games' => Lhc::getGames('zhengx'),
                ],
            ],
            //未完成
            'pl3' => [
                'yxdwdfs' => [
                    'title' => lang('一星定位胆复式')
                ],
            ],

        ];
        return $game[$type];
    }

    /**
     * @param array $row
     * @return bool|int|string
     */
    public static function add(array $row)
    {
        $newRow = Collection::elements([
            'name',
            'expect',
            'size',
            'quantity',
            'source',
            'timeOpen',
            'openCode',
            'sum',
            'type',
            'kills',
            'extra'
        ], $row);
        $newRow['extra'] = serialize($newRow['extra']);
        $newRow['timeCreated'] = time();
        $newRow = Collection::keyStyle($newRow, Collection::NAME_STYLE_C);
        try {
            return Db::table(self::$table)
                ->insertGetId($newRow);
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param $filter
     * @return array|Error
     */
    public static function fetch($filter,$connect = null)
    {
        $where = self::parseFilters($filter);
        try {
            if (empty($connect)) {
                $connect = Db::connect();
            }
            $row = $connect->table(self::$table)
                ->where($where)
                ->order('expect','desc')
                ->find();
            if (!empty($row)) {
                $row['time_created'] *= 1000;
                $row['time_open'] *= 1000;
                if (!empty($row['extra'])) {
                    $row['extra'] = unserialize($row['extra']);
                }
                return Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }

    /**
     * @param $filter
     * @return bool|Error
     */
    public static function remove($filter)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->delete();
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @param $newState
     * @return bool|Error
     */
    public static function update($filter, $newState)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->update($newState);
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @return array
     */
    public static function parseFilters($filter)
    {
        $where = [];
        if (is_array($filter)) {
            if (!empty($filter['id'])) {
                $where[] = ['id', '=', $filter['id']];
            }
            if (!empty($filter['name']) && !empty($filter['expect'])) {
                $where[] = ['name', '=', $filter['name']];
                $where[] = ['expect', '=', $filter['expect']];
            }
            if (!empty($filter['name']) && !empty($filter['order'])) {
                $where[] = ['name', '=', $filter['name']];
            }
            if (empty($where)) {
                throw error(-10, '参数错误必须指定id111');
            }
        } else {
            $where[] = ['id', '=', intval($filter)];
        }
        return $where;
    }

    /**
     * @param array $filters
     * @param int $pIndex
     * @param int $pSize
     * @param int $total
     * @return array|Error
     */
    public static function search(array $filters, int $pIndex = 0, int $pSize = 10, int &$total = 0)
    {
        $where = [];
        if (!empty($filters['name'])) {
            $where[] = ['name', '=', $filters['name']];
        }
        if (!empty($filters['expect'])) {
            $where[] = ['expect', '=', $filters['expect']];
        }
        if (!empty($filters['timeCreatedStart'])) {
            $where[] = ['time_created', '>=', $filters['timeCreatedStart']];
        }
        try {
            if (!empty($pIndex)) {
                $total = Db::table(self::$table)
                    ->where($where)
                    ->count();
            }
            $query = Db::table(self::$table)
                ->where($where);
            if (!empty($pIndex)) {
                $query->page($pIndex, $pSize);
            }
            if (!empty($filters['order'])) {
                $query->order($filters['order']);
            }
            $dataSet = $query->select();
            if (!$dataSet->isEmpty()) {
                return $dataSet->map(
                    function ($row) {
                        $row['time_created'] *= 1000;
                        $row['time_open'] *= 1000;
                        $row = Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
                        if (!empty($row['extra'])) {
                            $row['extra'] = unserialize($row['extra']);
                        }
                        return $row;
                    }
                )->toArray();
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }

}